﻿using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using CapstoneProjectAEReader.Entities.Models;

namespace CapstoneProjectAEReader.DataAccess.DBAccess
{
    public class FileHandler
    {
        public void WriteFile(string fileName, DTO dto)
        {
            Stream stream = File.Open(fileName, FileMode.OpenOrCreate);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, dto);
            stream.Close();
        }

        public DTO ReadFile(string fileName)
        {
            Stream stream = File.Open(fileName, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            DTO dto = (DTO)bFormatter.Deserialize(stream);
            stream.Close();
            if(dto.SettingDetail.RecentRead == null)
            {
                dto.SettingDetail.RecentRead = new List<Ebook>();
            }
            return dto;
        }
    }
}
